package com.jk.smartelderlycare.controller;

import com.jk.smartelderlycare.annotation.MyLog;
import com.jk.smartelderlycare.config.RedisCache;
import com.jk.smartelderlycare.dto.SysRole;
import com.jk.smartelderlycare.dto.SysUser;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.security.LoginUser;
import com.jk.smartelderlycare.service.SysUserServiceFeign;
import com.jk.smartelderlycare.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysUserController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/5 20:03
 */
@Api(tags = "员工管理模块")
@RestController
@CrossOrigin
@RequestMapping(value = "sysUser")
public class SysUserController {
    @Autowired
    private SysUserServiceFeign sysUserServiceFeign;

    @Autowired
    private RedisCache redisCache;

    /** 
     * @description: 员工列表后端分页查询
     * @param: [sysUser, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysUser>>
     * @author hhk
     * @date: 2024/11/5 20:12
     */ 
    @ApiOperation(value = "员工列表后端分页查询",httpMethod ="POST",notes = "员工列表后端分页查询")
    @PostMapping(value = "findSysUserPaginationList")
    @MyLog(url = "/sysUser/findSysUserPaginationList")
    public ResponseResult<PagedResponse<SysUser>> findSysUserPaginationList(@RequestBody SysUser sysUser, @RequestParam(value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "1")Integer pageSize) {
        return new ResponseResult<>(200,"查询成功",sysUserServiceFeign.findSysUserPaginationList(sysUser,pageNum,pageSize));
    }
    /** 
     * @description: 新增员工列表
     * @param: [sysUser]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/5 20:14
     */ 
    @ApiOperation(value = "新增员工列表",httpMethod = "POST",notes = "新增员工列表")
    @PostMapping(value = "addSysUser")
    @MyLog(url = "/sysUser/addSysUser")
    public ResponseResult addSysUser(@RequestBody SysUser sysUser) {
        try {
            //密码加密
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String newPassword = passwordEncoder.encode(sysUser.getPassword());
            sysUser.setPassword(newPassword);
            sysUserServiceFeign.addSysUser(sysUser);
            return new ResponseResult(200,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"新增失败");
        }
    }
    /** 
     * @description: 根据id删除一条数据
     * @param: [userId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/5 20:18
     */ 
    @ApiOperation(value = "根据id删除一条数据",httpMethod = "DELETE",notes = "根据id删除一条数据")
    @DeleteMapping(value = "deleteSysUserByUserId")
    @MyLog(url = "/sysUser/deleteSysUserByUserId")
    public ResponseResult deleteSysUserByUserId(@RequestParam Long userId){
        try {
            sysUserServiceFeign.deleteSysUserByUserId(userId);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"删除失败");
        }
    }
    /** 
     * @description: 根据id查询一条数据
     * @param: [userId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.dto.SysUser>
     * @author hhk
     * @date: 2024/11/5 20:20
     */ 
    @ApiOperation(value = "根据id查询一条数据",httpMethod = "GET",notes = "根据id查询一条数据")
    @GetMapping(value = "findSysUserByUserId")
    @MyLog(url = "/sysUser/findSysUserByUserId")
    public ResponseResult<SysUser>findSysUserByUserId(@RequestParam Long userId){
        return new ResponseResult<>(200,"回显成功",sysUserServiceFeign.findSysUserByUserId(userId));
    }
    /** 
     * @description: 修改员工列表
     * @param: [sysUser]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/5 20:54
     */ 
    @ApiOperation(value = "修改员工列表",httpMethod = "PUT",notes = "修改员工列表")
    @PutMapping(value = "updateSysUser")
    @MyLog(url = "/sysUser/updateSysUser")
    public ResponseResult updateSysUser(@RequestBody SysUser sysUser){
        try {
            LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            sysUser.setModifyDate(new Date());
            sysUser.setModifyUser(loginUser.getUser().getUserName());
            sysUserServiceFeign.updateSysUser(sysUser);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }
    /**
     * @description: 状态修改
     * @param: [sysUser]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/6 20:35
     */
    @ApiOperation(value = "状态修改",httpMethod = "POST",notes = "")
    @PostMapping("updateStatusById")
    @MyLog(url = "/sysUser/updateStatusById")
    public ResponseResult updateStatusById(@RequestBody SysUser sysUser){
        try {
            sysUserServiceFeign.updateStatusById(sysUser);
            return new ResponseResult(200,"状态修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"状态修改失败");
        }
    }
    /** 
     * @description: 角色动态下拉列表查询
     * @param: []
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<java.util.List<com.jk.smartelderlycare.dto.SysRole>>
     * @author hhk
     * @date: 2024/11/6 20:54
     */ 
    @ApiOperation(value = "角色查询",httpMethod = "GET",notes = "")
    @GetMapping("findPoleList")
    @MyLog(url = "/sysUser/findPoleList")
    public ResponseResult<List<SysRole>> findSysRoleList(){
        try {
            List<SysRole> roleList = sysUserServiceFeign.findSysRoleList();
            return new ResponseResult(200,"查询角色成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"查询角色失败");

        }
    }
    /** 
     * @description: 上传图片
     * @param: [file, request]
     * @return: java.lang.String
     * @author hhk
     * @date: 2024/11/7 10:54
     */
    @ApiOperation(value = "上传图片",httpMethod = "POST",notes = "上传图片")
    @PostMapping("uploadImg")
    public String uploadImg(MultipartFile file, HttpServletRequest request) {
        return FileUtil.uploadFile(file,request);
    }
    /**
     * @description: 根据当前登陆人id查询
     * @param: [sysUser]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.dto.SysUser>
     * @author hhk
     * @date: 2024/11/11 10:27
     */
    @ApiOperation(value = "根据当前登陆人id查询",httpMethod = "POST",notes = "根据当前登陆人id查询")
    @PostMapping(value = "getSysUserByUsername")
    public ResponseResult<List<SysUser>> getSysUserByUsername(@RequestBody SysUser sysUser) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        sysUser.setUserId(loginUser.getUser().getUserId());
            return new ResponseResult<>(200,"查询成功",sysUserServiceFeign.getSysUserByUsername(sysUser));

    }
    /** 
     * @description: 修改密码
     * @param: [sysUser]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/11 11:45
     */ 
    @ApiOperation(value = "修改密码",httpMethod = "PUT",notes = "修改密码")
    @PutMapping("upSysUserByPassWord")
    public ResponseResult upSysUserByPassWord(@RequestBody SysUser sysUser){
        try {
            LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            //密码加密
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean b = passwordEncoder.matches(sysUser.getPassword(), loginUser.getPassword());//判断密码 前面参数是明文密码 后面是加密密码
            if (b) {
                sysUser.setPassword(passwordEncoder.encode(sysUser.getAddPassword()));
                Long userId = loginUser.getUser().getUserId();
                sysUser.setUserId(userId);
                sysUserServiceFeign.upSysUserByPassWord(sysUser);
                redisCache.deleteObject("login:" + userId);
                return new ResponseResult(200,"密码修改成功");
            } else {
                return new ResponseResult(500, "密码不正确");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"密码修改失败");
        }
    }
    @ApiOperation(value = "用户表单表修改",httpMethod ="POST",notes = "用户表单表修改")
    @PostMapping(value = "uptSysUser")
    public ResponseResult uptSysUser(@RequestBody SysUser sysUser){
        try {
            sysUserServiceFeign.uptSysUser(sysUser);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }

    }
    /**
     * @description: 角色表单表新增
     * @param: [sysRole]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/12 20:40
     */
    @ApiOperation(value = "角色表单表新增",httpMethod ="POST",notes = "角色表单表新增")
    @PostMapping(value = "addSysRole")
    private ResponseResult addSysRole(@RequestBody SysRole sysRole){
        try {
            sysUserServiceFeign.addSysRole(sysRole);
            return new ResponseResult(200,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"新增失败");
        }
    }








}
